/*
给定一个单链表，其中的元素按升序排序，将其转换为高度平衡的二叉搜索树。

本题中，一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

示例:

给定的有序链表： [-10, -3, 0, 5, 9],

一个可能的答案是：[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树：

      0
     / \
   -3   9
   /   /
 -10  5

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
*/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    bool DEBUG=false;
public:
    TreeNode* sortedListToBST(ListNode* head) {
        if(head==NULL){
            return NULL;
        }
        ListNode* mid_pre=Mid_pre(head);
        if(DEBUG){
            cout<<"list  mid_pre="<<mid_pre->val<<endl;
        }
        ListNode* mid=mid_pre->next;
        if(DEBUG&&mid!=NULL){
            cout<<"list  mid="<<mid->val<<endl;
        }
        mid_pre->next=NULL;
        ListNode* next_head=NULL;
        TreeNode* root=NULL;
        if(mid!=NULL){
            next_head=mid->next;
            mid->next=NULL;
        
            root=new TreeNode(mid->val);
            root->left=sortedListToBST(head);
            root->right=sortedListToBST(next_head);
        }else{
            root=new TreeNode(mid_pre->val);
        }
        return root;
    }

    ListNode* Mid_pre(ListNode* head){
        ListNode* slow=head;
        ListNode* fast=head;
        ListNode* mid_pre=head;
        while(fast!=NULL&&fast->next!=NULL){
            mid_pre=slow;
            slow=slow->next;
            fast=fast->next->next;
        }
        return mid_pre;
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    bool DEBUG=false;
public:
    TreeNode* sortedListToBST(ListNode* head) {
        if(head==NULL){
            return NULL;
        }
        if(head->next==NULL){
            return new TreeNode(head->val);
        }
        ListNode* mid_pre=Mid_pre(head);
        if(DEBUG){
            cout<<"list  mid_pre="<<mid_pre->val<<endl;
        }
        ListNode* mid=mid_pre->next;
        if(DEBUG&&mid!=NULL){
            cout<<"list  mid="<<mid->val<<endl;
        }
        mid_pre->next=NULL;
        ListNode* next_head=mid->next;

        mid->next=NULL;
        
        TreeNode* root=new TreeNode(mid->val);
        root->left=sortedListToBST(head);
        root->right=sortedListToBST(next_head);
        return root;
    }

    ListNode* Mid_pre(ListNode* head){
        ListNode* slow=head;
        ListNode* fast=head;
        ListNode* mid_pre=head;
        while(fast!=NULL&&fast->next!=NULL){
            mid_pre=slow;
            slow=slow->next;
            fast=fast->next->next;
        }
        return mid_pre;
    }
};